package com.jiankang.askhealth.core.transmitter.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.jiankang.askhealth.annotation.Permission;
import com.jiankang.askhealth.aware.UserAware;
import com.jiankang.askhealth.base.custom.PaginationResult;
import com.jiankang.askhealth.base.web.PagingAction;
import com.jiankang.askhealth.constant.AreaType;
import com.jiankang.askhealth.constant.UserType;
import com.jiankang.askhealth.core.area.service.AreaService;
import com.jiankang.askhealth.core.transmitter.custom.TransmitterSearchBean;
import com.jiankang.askhealth.core.transmitter.domain.Transmitter;
import com.jiankang.askhealth.core.transmitter.service.TransmitterService;
import com.jiankang.askhealth.core.user.bean.UserBean;

public class TransmitterAction extends PagingAction implements ServletRequestAware, ServletResponseAware, UserAware {

	private UserBean loginUser;

    private Integer transmitterId;
	
	private Transmitter transmitter; 
	private TransmitterSearchBean transmitterSearchBean;

	private HttpServletRequest request;
	private HttpServletResponse response;
	
	private AreaService areaService;
	private TransmitterService transmitterService;
	

    @Permission(userType = UserType.SUPER_ADMIN)
	public String addTransmitter() {
    	if (request.getParameter("submit") == null) {
    		request.setAttribute("contientAreas", areaService.getAreasByType(AreaType.CONTIENT.getType()));
			return INPUT;
		}
    	
    	transmitter.setUserId(loginUser.getUserId());
    	
    	transmitterService.addTransmitter(transmitter);
    	
		return SUCCESS;
	}

    @Permission(userType = UserType.SUPER_ADMIN)
    public String modifyTransmitter() {
		if (request.getParameter("submit") == null) {
    		request.setAttribute("contientAreas", areaService.getAreasByType(AreaType.CONTIENT.getType()));
			transmitter = transmitterService.getByPk(transmitterId);
			return INPUT;
		}
		transmitterService.update(transmitter);
		return SUCCESS;
	}
    
    @Permission(userType = UserType.SUPER_ADMIN)
	public String listTransmitter() {
    	if (getPageIndex() == 0) {
            setPageIndex(1);
        }
        int start = getPageSize() * (getPageIndex() - 1);

    	PaginationResult<Transmitter> paginationResult = transmitterService.getTransmitterList(transmitterSearchBean, start, getPageSize());
    	request.setAttribute("objs", paginationResult.getList());
        setTotal(paginationResult.getTotal());
        setPageNum();
        return SUCCESS;
    }
	
	@Override
	public void setLoginUser(UserBean userBean) {
		this.loginUser = userBean;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setTransmitterId(Integer transmitterId) {
		this.transmitterId = transmitterId;
	}

	public Transmitter getTransmitter() {
		return transmitter;
	}

	public void setTransmitter(Transmitter transmitter) {
		this.transmitter = transmitter;
	}

	public void setTransmitterSearchBean(TransmitterSearchBean transmitterSearchBean) {
		this.transmitterSearchBean = transmitterSearchBean;
	}

	public void setTransmitterService(TransmitterService transmitterService) {
		this.transmitterService = transmitterService;
	}

	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}
	
	

}
